package com.example.pceb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pceb.entity.Enterprise;
import com.example.pceb.entity.Recruit;
import com.example.pceb.mapper.EnterpriseMapper;
import com.example.pceb.result.Result;
import com.example.pceb.service.EnterpriseService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/enterprises")
//@CrossOrigin(origins = "*")
public class EnterPriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    //根据用户名查企业名
    //根据id或用户名查企业信息
    @GetMapping("info")
    public Result<Enterprise> getEnterpriseInfo(@RequestParam(required = false) String id, @RequestParam(required = false) String username) {
        Enterprise enterprise = null;
        if (id != null && !id.isEmpty()) {
            enterprise = enterpriseService.getById(id);
        } else if (username != null && !username.isEmpty()) {
            enterprise = enterpriseService.getByUsername(username);
        }
        return Result.success(enterprise);
    }

    //获取公司所有招聘信息
    @GetMapping("all")
    public Result<List<Recruit>>  all(String enterprise) {
        return enterpriseService.getRecruitsByEnterprise(enterprise);
    }

    //根据id删除本公司招聘信息
    @DeleteMapping("del")
    public Result<String> del(int recruitId) {
        return enterpriseService.deleteRecruit(recruitId);
    }

    //公司更新招聘信息
    @PutMapping("edit")
    public Result<String> edit(@RequestBody Recruit recruit) {
        return enterpriseService.editRecruit(recruit);
    }

    //公司新增招聘信息
    @PostMapping("add")
    public Result<String> add(@RequestBody Recruit recruit) {
        return enterpriseService.addRecruit(recruit);
    }

    // 获取所有企业ID
    @GetMapping("all-ids")
    public Result<List<String>> getAllEnterpriseIds() {
        List<String> ids = enterpriseService.getAllEnterpriseIds();
        return Result.success(ids);
    }

    //公司展示地理位置
    @GetMapping("location")
    public Map<String, Object> getAllEnterprises() {
        List<Map<String, String>> list = enterpriseService.getAllEnterprisesForMap();
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", list);
        return result;
    }

    @GetMapping("/locations")
    public List<Enterprise> getAllWithLocation() {
        return enterpriseMapper.selectList(
                new QueryWrapper<Enterprise>().isNotNull("lng").isNotNull("lat")
        );
    }
}
